/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * add_menu.java
 *
 * Created on Jul 25, 2011, 1:01:35 AM
 */
package introse1;

import java.util.ArrayList;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.*;
import javax.swing.table.TableModel.*;
import javax.swing.JTable.*;
import java.sql.ResultSet;

/**
 *
 * @author Trisha
 */
public class add_menu extends javax.swing.JFrame {

    /** Creates new form add_menu */
    public add_menu(manage_menu manage_menu) {
        initComponents();
        manmenu = new MenuManager();
        ResultSet res;
        DefaultTableModel tmodel = (DefaultTableModel)IngredientTable.getModel();
        try{
            res = manmenu.getIngredients();
            while(res.next()){
                String[] s1 = {res.getString("inventoryName")};
                tmodel.addRow(s1);
            }
        }catch(Exception e){}
        this.manage_menu = manage_menu;
        manage_menu.setVisible(false);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        FoodName = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        IngredientTable = new javax.swing.JTable();
        DeleteIngredientButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        recipeTable = new javax.swing.JTable();
        AddRecipeButton = new javax.swing.JButton();
        SaveMenuButton = new javax.swing.JButton();
        BackButton = new javax.swing.JButton();
        PriceOfFood = new javax.swing.JFormattedTextField();
        FoodNameError = new javax.swing.JLabel();
        PriceError = new javax.swing.JLabel();
        RecipeTableError = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Rockwell", 0, 24));
        jLabel1.setText("Add Menu");

        jLabel2.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel2.setText("Name of Food:");

        jLabel3.setFont(new java.awt.Font("Rockwell", 0, 18));
        jLabel3.setText("Price of Food:");

        IngredientTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
            },
            new String [] {
                "Ingredient"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        IngredientTable.setSelectionMode(1);
        jScrollPane1.setViewportView(IngredientTable);

        DeleteIngredientButton.setFont(new java.awt.Font("Rockwell", 0, 18));
        DeleteIngredientButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/remove.png"))); // NOI18N
        DeleteIngredientButton.setBorder(null);
        DeleteIngredientButton.setBorderPainted(false);
        DeleteIngredientButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteIngredientButtonActionPerformed(evt);
            }
        });

        recipeTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Ingredient", "Quantity", "Measure"
            }
        ));
        jScrollPane2.setViewportView(recipeTable);

        AddRecipeButton.setText("  >>>> ");
        AddRecipeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddRecipeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(DeleteIngredientButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(AddRecipeButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(109, 109, 109))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(AddRecipeButton)
                        .addGap(31, 31, 31)
                        .addComponent(DeleteIngredientButton)))
                .addContainerGap())
        );

        SaveMenuButton.setFont(new java.awt.Font("Rockwell", 0, 18));
        SaveMenuButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/save.png"))); // NOI18N
        SaveMenuButton.setBorder(null);
        SaveMenuButton.setBorderPainted(false);
        SaveMenuButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SaveMenuButtonActionPerformed(evt);
            }
        });

        BackButton.setFont(new java.awt.Font("Rockwell", 0, 18));
        BackButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse1/back.png"))); // NOI18N
        BackButton.setBorder(null);
        BackButton.setBorderPainted(false);
        BackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(PriceOfFood)
                            .addComponent(FoodName, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(FoodNameError, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(PriceError, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 647, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(SaveMenuButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(BackButton))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(RecipeTableError, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(170, 170, 170)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(FoodName, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(FoodNameError))
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(PriceOfFood, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(PriceError))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(RecipeTableError)
                .addGap(4, 4, 4)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SaveMenuButton)
                    .addComponent(BackButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 594, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(30, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    //TODO make sql stuff, save to database
    private void DeleteIngredientButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteIngredientButtonActionPerformed
        try{
            DefaultTableModel tmodel = (DefaultTableModel)recipeTable.getModel();
            
            int x = recipeTable.getSelectedRow();
            tmodel.removeRow(x);
        }catch(Exception e){
            RecipeTableError.setText("No Row Selected");
        }
    }//GEN-LAST:event_DeleteIngredientButtonActionPerformed

    private void BackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackButtonActionPerformed
        this.setVisible(false);
        manage_menu.setVisible(true);
    }//GEN-LAST:event_BackButtonActionPerformed

    private void SaveMenuButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveMenuButtonActionPerformed
        manmenu = new MenuManager();
        int x = recipeTable.getRowCount();
        vars.setName_of_food(FoodName.getText());
        if(vars.getName_of_food().isEmpty()){
            FoodNameError.setText("Error no Name input");
        }
        if(manmenu.checkExists(vars.getName_of_food()) == 0){
            try{
                vars.setPrice_of_food(Float.parseFloat(PriceOfFood.getText()));
            }catch(NumberFormatException e){
                PriceError.setText("Input must be a number");
            }
            if(x > 0){
                manmenu.AddToMenu(this);
                for(int i = 0; i < x; i++){
                    String s1 = (String)recipeTable.getModel().getValueAt(i, 0).toString();
                    Float f2 = Float.parseFloat((String)recipeTable.getModel().getValueAt(i,1).toString());
                    vars.setIngredient_name(s1);
                    vars.setRecipe_quantity(f2);
                    manmenu.Addtorecipelist(this);
                }
                manage_menu.setTable(vars);   
            }
            else{
               RecipeTableError.setText("No ingredients");
            }
        }
        else if(manmenu.checkExists(vars.getName_of_food()) == 1){
            FoodNameError.setText("Food exists already");
        }
        else if(manmenu.checkExists(vars.getName_of_food()) == -1){
            RecipeTableError.setText("Error!");
        }
    }//GEN-LAST:event_SaveMenuButtonActionPerformed
    
    public void setRecipe_table(){
        DefaultTableModel tmodel;
        tmodel = (DefaultTableModel)recipeTable.getModel();
        String[] s1 = {vars.getIngredient_name(), Float.toString(vars.getRecipe_quantity()), vars.getUnit_of_measure()};
        tmodel.addRow(s1);
    }
    
    public void setVars(VariableList listahan){
        vars = listahan;
    }
    
    public VariableList getVars(){
        return vars;
    }
    private void AddRecipeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddRecipeButtonActionPerformed
        try{
            String str = (String)IngredientTable.getModel().getValueAt(IngredientTable.getSelectedRow(), 0);
            vars.setIngredient_name(str);
            this.setVisible(false);
            add_recipe next = new add_recipe(this);
            next.setVisible(true);
        }catch(Exception e){
            RecipeTableError.setText("No Row Selected");
        }
    }//GEN-LAST:event_AddRecipeButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new add_menu(manage_menu).setVisible(true);
            }
        });
    }
    
    public static String[] s3 = new String[100];
    private static add_recipe add_recipe;
    private static manage_menu manage_menu;
    private static VariableList vars = new VariableList();
    public static MenuManager manmenu;
    public static ResultSet res;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddRecipeButton;
    private javax.swing.JButton BackButton;
    private javax.swing.JButton DeleteIngredientButton;
    private javax.swing.JTextField FoodName;
    private javax.swing.JLabel FoodNameError;
    private javax.swing.JTable IngredientTable;
    private javax.swing.JLabel PriceError;
    private javax.swing.JFormattedTextField PriceOfFood;
    private javax.swing.JLabel RecipeTableError;
    private javax.swing.JButton SaveMenuButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable recipeTable;
    // End of variables declaration//GEN-END:variables
}
